/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pattern.dao;

import com.pattern.model.Karyawan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rully Andhika
 */
public class KaryawanDAO {
    
    private Connection koneksi;
    private PreparedStatement insert;
    private PreparedStatement update;
    private PreparedStatement delete;
    private PreparedStatement getByNik;
    private PreparedStatement getAll;
    
    private final String insertQuery = "INSERT INTO karyawan(nama, jabatan, bagian) "
            + "VALUES(?,?,?)";
    private final String updateQuery = "UPDATE karyawan SET nama=?, jabatan=?, bagian=? "
            + "WHERE nik=?";
    private final String deleteQuery = "DELETE FROM karyawan WHERE nik=?";
    private final String getByNikQuery = "SELECT * FROM karyawan WHERE nik=?";
    private final String getAllQuery = "SELECT * FROM karyawan";
    
    public void setConnection(Connection koneksi) throws SQLException {
        this.koneksi = koneksi;
        insert = this.koneksi.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
        update = this.koneksi.prepareStatement(updateQuery);
        delete = this.koneksi.prepareStatement(deleteQuery);
        getByNik = this.koneksi.prepareStatement(getByNikQuery);
        getAll = this.koneksi.prepareStatement(getAllQuery);
    }
    // Insert and Update
    public Karyawan save(Karyawan karyawan) throws SQLException {
        // Insert
        if(karyawan.getNik() == null) {
            //insert.setInt(1, karyawan.getNik());
            insert.setString(1, karyawan.getNama());
            insert.setString(2, karyawan.getJabatan());
            insert.setString(3, karyawan.getBagian());
            
            int id = insert.executeUpdate();
            karyawan.setNik(id);
        }
        // Update
        else {
            update.setString(1, karyawan.getNama());
            update.setString(2, karyawan.getJabatan());
            update.setString(3, karyawan.getBagian());
            update.executeUpdate();
        }
        return karyawan;  
    }
    // Delete
    public Karyawan delete(Karyawan karyawan) throws SQLException {
        delete.setInt(1, karyawan.getNik());
        delete.executeUpdate();
        return karyawan;   
    }
    // Cari Data Karyawan Berdasarkan NIK
    public Karyawan findByNik(Integer nik) throws SQLException {
        getByNik.setInt(1, nik);
        ResultSet rs = getByNik.executeQuery();
        
        if(rs.next()) {
            Karyawan karyawan = new Karyawan();
            karyawan.setNik(rs.getInt("nik"));
            karyawan.setNama(rs.getString("nama"));
            karyawan.setJabatan(rs.getString("jabatan"));
            karyawan.setBagian(rs.getString("bagian"));
            return karyawan;
        }
        return null;    
    }
    
    public List<Karyawan> getAll() throws SQLException {
        List<Karyawan> data = new ArrayList<Karyawan>();
        ResultSet rs = getAll.executeQuery();
        
        while (rs.next()) {
            Karyawan karyawan = new Karyawan();
            karyawan.setNik(rs.getInt("nik"));
            karyawan.setNama(rs.getString("nama"));
            karyawan.setJabatan(rs.getString("jabatan"));
            karyawan.setBagian(rs.getString("bagian"));
            data.add(karyawan);
        }
        return data;  
    }
}
